package com.xuelang.config;

import cn.hutool.core.util.ObjectUtil;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.util.ServletUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Feign配置类
 *
 * @author xiwen.zxg
 * @since 2024/5/29
 */
@Configuration
public class FeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest();
        if (ObjectUtil.isNotNull(httpServletRequest)) {
            Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);

            String authentication = headers.get(Constant.REQUEST_HEADER_NAME_Authorization);
            if (StringUtils.isNotEmpty(authentication)) {
                requestTemplate.header(Constant.REQUEST_HEADER_NAME_Authorization, authentication);
            }
        }
    }
}
